package com.wyiot.pet.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wyiot.pet.mapper.UserMapper;
import com.wyiot.pet.model.entity.UserDO;
import com.wyiot.pet.model.dto.UserDTO;
import com.wyiot.core.utils.Md5Util;
import com.wyiot.core.exception.ValidateException;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * 用户Service
 *
 * @author yly
 * @since 2022年03月28日
 */
@Service
public class UserService extends ServiceImpl<UserMapper, UserDO> {


    /**
     * 用户注册
     *
     * @see UserDTO
     */
    public void userRegistration(UserDTO userDTO) {
        if (baseMapper.isUserCodeExist(userDTO.getUserCode()) || baseMapper.isUserPhoneExist(userDTO.getUserPhone())) {
            throw ValidateException.of("账号或手机号已存在");
        }
        UserDO userDO = new UserDO();
        BeanUtils.copyProperties(userDTO, userDO);
        userDTO.setUserPassword(Md5Util.code(userDTO.getUserPassword()))  ;
        save(userDO);
    }

}


